package com.company.cloud.acct.modules.mgr.controller;

import com.company.cloud.acct.modules.base.entity.CashConfigInfo;
import com.company.cloud.acct.modules.base.service.CashConfigInfoService;
import com.company.cloud.acct.modules.cache.models.CashConfigModel;
import com.company.cloud.acct.modules.cache.service.AcctCacheService;
import com.company.cloud.acct.modules.cache.utils.AcctConvertUtils;
import com.company.cloud.acct.modules.mgr.controller.req.CashConfigSaveParam;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Authc
@RestController
@RequestMapping("/mgr/acct/cashConfig")
@Api(tags="Acct_MGR_提现配置")
public class AcctCashConfigMgrController {
    @Autowired
    private CashConfigInfoService cashConfigInfoService;
    @Autowired
    private AcctCacheService acctCacheService;


    @PostMapping("/add2edit")
    @StaffLogc(title = "Acct_MGR_提现配置新增/修改配置", action = "Acct_MGR_提现配置新增/修改配置", actionAdo = "Acct_MGR_提现配置")
    @ApiOperation(value="新增/修改配置",response = BaseResult.class)
    public BaseResult add2edit(@RequestBody CashConfigSaveParam param) {
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        CashConfigInfo entity = cashConfigInfoService.getById(userInfo.getCompanyNo());
        if (null == entity) {
            entity = AcctConvertUtils.getCashConfigInfo(param, null);
            entity.setCompanyNo(userInfo.getCompanyNo());
            entity.addCreateParam(userInfo);
            entity.addEditParam(userInfo);
            cashConfigInfoService.save(entity);

        } else {
            entity = AcctConvertUtils.getCashConfigInfo(param, entity);
            entity.addEditParam(userInfo);
            cashConfigInfoService.updateById(entity);
        }
        return BaseResult.ok();
    }

    @GetMapping("/get")
    @ApiOperation(value="查询配置信息",response = CashConfigModel.class)
    public BaseResult get() {
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        CashConfigInfo entity = cashConfigInfoService.getById(userInfo.getCompanyNo());
        if (null == entity) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        return BaseResult.ok(AcctConvertUtils.getCashConfigModel(entity));
    }
}
